package xiaodou.com.testinterface.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import xiaodou.com.testinterface.R;

/**
 * Created by vae on 2018-03-03.
 */

public class IPDialog extends Dialog {
    private Context context;
    private Handler handler;
    private Button okButton,cancelButton;
    private EditText ipEditText1,ipEditText2,ipEditText3,ipEditText4,port;
    private SharedPreferences sp;

    public IPDialog(@NonNull Context context) {
        super(context);
        this.context = context;
    }

    public IPDialog(@NonNull Context context,Handler handler){
        super(context);
        this.context = context;
        this.handler = handler;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_layout);
        init();
        lisener();
    }

    private void init() {
        okButton = (Button) findViewById(R.id.ok);
        cancelButton = (Button) findViewById(R.id.cancel);
        ipEditText1 = (EditText) findViewById(R.id.ipEditText1);
        ipEditText2 = (EditText) findViewById(R.id.ipEditText2);
        ipEditText3 = (EditText) findViewById(R.id.ipEditText3);
        ipEditText4 = (EditText) findViewById(R.id.ipEditText4);
        port = (EditText) findViewById(R.id.port);
        sp = context.getSharedPreferences("saveInfos",0);
        String ip = sp.getString("ip", "").toString();
        Log.i("IPDialog",ip);
        if (!ip.equals("")&&ip!=null){
         String[] split = ip.split("\\.");
            ipEditText1.setText(split[0]);
            ipEditText2.setText(split[1]);
            ipEditText3.setText(split[2]);
            ipEditText4.setText(split[3]);
            port.setText(split[4]);
        }
    }

    private void lisener() {
        okButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String ipStr = ipEditText1.getText().toString() + "." + ipEditText2.getText().toString() + "."
                        + ipEditText3.getText().toString() + "." + ipEditText4.getText().toString();
                if (ipStr.equals("") || ipStr == null) {
                    Toast.makeText(getContext(), "无效的ip地址", Toast.LENGTH_LONG).show();
                    return;
                } else {
                    Pattern p = Pattern.compile(
                            "(2[5][0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})");
                    Matcher m = p.matcher(ipStr);
                    if (!m.matches()) {
                        Toast.makeText(getContext(), "无效的ip地址", Toast.LENGTH_LONG).show();
                        return;
                    }
                }
                if (port.getText().toString().trim().equals("")){
                    Toast.makeText(getContext(), "端口不能为空", Toast.LENGTH_LONG).show();
                    return;
                }
                Toast.makeText(getContext(), "设置成功", Toast.LENGTH_SHORT).show();
                Message msg = Message.obtain();
                msg.obj = ipEditText1.getText().toString().trim() + "." + ipEditText2.getText().toString().trim() + "."
                        + ipEditText3.getText().toString().trim() + "." + ipEditText4.getText().toString().trim()+"."+port.getText().toString().trim();
                handler.sendMessage(msg);
                IPDialog.this.dismiss();
            }
        });

        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IPDialog.this.dismiss();
            }
        });
    }

}
